package client.command.handle.impl;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.List;

import server.help.HelpContext;
import client.command.handle.ClientCommandHandler;

public class CommandHandler implements ClientCommandHandler {

	@Override
	public StringBuilder command(List<String> commandList) throws IOException {
		// TODO Auto-generated method stub
		Socket socket=new Socket("127.0.0.1",8887);
		OutputStream os=socket.getOutputStream();
		InputStream is=socket.getInputStream();
		BufferedReader bufR=new BufferedReader(new InputStreamReader(is));
		System.out.println(bufR.readLine());
		StringBuilder sb=new StringBuilder();
		switch(commandList.get(0))
		{
		case "cp" :  sb=this.handleCP(commandList,bufR,os);
			break;
		case "scp":  sb=this.handleSCP(commandList,bufR,os);
			break;
		case "ls" :  sb=this.handleLS(commandList,bufR,os);
			break;
		case "rm" :  sb=this.handleRM(commandList,bufR,os);
			break;
		case "q"  :  sb=this.handleQuit(commandList);
			break;
		default   :  sb=this.handleHelp(commandList);
		}
		return null;
	}

	@Override
	public  StringBuilder handleCP(List<String> list,BufferedReader bufR,OutputStream os) throws  IOException {
		// TODO Auto-generated method stub
		StringBuilder sb=new StringBuilder();
		os.write(("cp"+"\n").getBytes());
		os.write((list.get(2)+"\n").getBytes());
		String input="";
		while((input=bufR.readLine())!=null)
		 {
			 if(input.startsWith("file found"))
			 {
				 //get local Path
				 String lp=list.get(1);
//				 System.out.println("client get file: "+list.get(3));
				 OutputStream fileOutput = new FileOutputStream(lp);
				 System.out.println("file creat success");
				 while((input=bufR.readLine())!=null)
				 {
					 System.out.println(input);
					 if(input.equals("file end"))
						 break;
					 fileOutput.write(input.getBytes());
					 fileOutput.write("\n".getBytes());
				 }
				 fileOutput.close();
				 sb.append("copy succeed!\n");
			 }
			 else
			 {
				 System.out.println("the file you want not found ! Correct the name or path and input what you want");
				 sb.append("copy failed!\n");
			 }
			 break;
		 }
		return sb;
	}

	@Override
	public StringBuilder handleSCP(List<String> list,BufferedReader bufR,OutputStream os) throws IOException {
		// TODO Auto-generated method stub
		StringBuilder sb=new StringBuilder();
		//get local Path
		os.write(("scp"+"\n").getBytes());
		os.write((list.get(2)+"\n").getBytes());
		String lp=list.get(2);
		System.out.println("client delivery file: "+lp);
		//catch server local file and delivery
		InputStream fileInput = null;
		try {
			fileInput = new FileInputStream(lp);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			os.write("not found file\n".getBytes());
			sb.append("scp failed because of not find local file");
		}
		os.write("file found\n".getBytes());
		byte[] buf=new byte[fileInput.available()];
		int m=0;
		while((m=fileInput.read(buf))!=-1)
		      os.write(buf);
		os.write("file end\n".getBytes());
		fileInput.close();  //release temporary InputStream
		return sb;
	}

	@Override
	public StringBuilder handleLS(List<String> list,BufferedReader bufR,OutputStream os) {
		// TODO Auto-generated method stub
		StringBuilder sb=new StringBuilder();
		return null;
	}

	@Override
	public StringBuilder handleRM(List<String> list,BufferedReader bufR,OutputStream os) {
		// TODO Auto-generated method stub
		StringBuilder sb=new StringBuilder();
		return null;
	}

	@Override
	public StringBuilder handleQuit(List<String> list) {
		// TODO Auto-generated method stub
		System.exit(0);
		return null;
	}

	@Override
	public StringBuilder handleHelp(List<String> list) {
		// TODO Auto-generated method stub
		return HelpContext.helpDocument();
	}

}
